<?php
    $year = date('Y');
    $month = date('n');
    $days = getMonthDays($month);
    $title = "李学士的$year 年 $month 月的日历";
    $firstDay_date = "$year-$month-1";
    $firstDay_timestamp = strtotime($firstDay_date);
    $firstDay = getTimestamp2Number($firstDay_timestamp);
    
    function getTimestamp2Number($time){
        return date('w',$time);
    }
    function getMonthDays($month){
        switch($month){
            case 2:
                $days = ($year % 400 == 0 || $year %4 == 0 && $year %100 !=0)?29:28;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                $days = 30;
                break;
            default:
                $days = 31;
        }
        return $days;
    }
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>calender</title>
</head>
<body>
    <table width="400" cellpadding="0" cellspacing="2" align="center" >
        <caption><?php echo $title;?></caption>
        <tr>
            <th width="50" height="40">日</th>
            <th width="50" height="40">一</th>
            <th width="50" height="40">二</th>
            <th width="50" height="40">三</th>
            <th width="50" height="40">四</th>
            <th width="50" height="40">五</th>
            <th width="50" height="40">六</th>
        </tr>
        <?php
            echo '<tr>';
            for($i = 0;$i<$firstDay;$i++){
                echo '<td align="center" height="30"></td>';
            }
            for($j = 1;$j<$days;$j++){
                //
                if(($j + $firstDay -1) % 7 == 0){
                    echo '</tr></tr>';
                }
                //判断是否是今天
                if($j == date('j')){
                    echo '<td align="center" height="30" style="border:1px solid #f00;">'.$j.'</td>';
                }else{
                    echo '<td align="center" height="30">'.$j.'</td>';
                }
            }
            echo '</tr>';
        ?>
    </table>
</body>
</html>